(function ($app) {
    $app.module('gtPartyApp').controller('leaderAdminListCtrl', function ($scope, $http, $modal, $ocLazyLoad, $alert, $loading, $state, $bsRouterState, AuthUser, $timeout, $filter) {
        $state.params = $bsRouterState.$params($scope);
        $scope.loginUserId = AuthUser.getUser().Id;

        //定义数据集合
        $scope.dataList = []; //列表数据

        //查询条件
        $scope.selectparams = {
            pageindex: 1,
            pagesize: 10,
            userName: ''
        };
        $scope.pageInfo = {ptotal: 0};

        //加载列表数据
        $scope.getList = function () {
            $http
            ({
                method: 'get', url: '../../api/leaderadmin/getList', params: $scope.selectparams
            }).then(function (result) {
                $scope.dataList = result.data.item.list;
                $scope.pageInfo.ptotal = result.data.item.total;
            }, function (res) {
                $scope.showMsg('错误', '服务器错误');
            });
        };

        $scope.search = function () {
            $scope.getList();
        };

        var editCtrl = function ($scope, $http) {
            $scope.selectUser = {
                url: '../../api/partyUser/getPartyUserList',
                title: '人员选择',
                isMulti: true,
                selectData: {xm: '姓名', szdzb: '所在组织架构'},
                columns: {XM: '姓名', XBMC: '性别', JG: '籍贯', DZZMC: '所在组织架构'},
                selectLabelKey: 'XM',
                selectValuekey: 'RYBM',
                selectedValueData: '',
                selectedLabeData: '',
                selectedJData: [],
                selectParams: {
                    ryzt: '2,3',
                    dzzdm: AuthUser.getUser().DataDzzdm
                }
            };
            $scope.save = function (isflag) {
                if (isflag) {
                    $loading.show();
                    $http({
                        method: "post",
                        url: "../../api/leaderadmin/save",
                        data: {
                            userIds: $scope.selectUser.selectedValueData
                        }
                    }).then(function (result) {
                        $loading.hide();
                        if (result.data.success) {
                            $scope.showMsg('成功', result.data.msg);
                            $scope.getList();
                            $scope.$hide();
                        } else {
                            $scope.showMsg('失败', result.data.msg);
                        }
                    }, function (resp) {
                        $loading.hide();
                        $scope.showMsg('错误', '服务器错误');
                    });
                }
            }
        };
        editCtrl.$inject = ['$scope', '$http'];
        //定义模态框
        var editModal = $modal({
            resolve: {
                load: ['$ocLazyLoad', function ($ocLazyLoad) {
                }]
            },
            scope: $scope,
            controller: editCtrl,
            templateUrl: '../main/systemManage/leaderAdmin/edit.html',
            show: false,
            animation: 'am-fade-and-slide-top'
        });

        $scope.add = function () {
            editModal.$promise.then(editModal.show);
        };

        $scope.delete = function () {
            var ids = $.map($filter('filter')($scope.dataList, {rowChecked: true}), function (x) {
                return x.RYBM;
            });
            if (ids.length <= 0) {
                $scope.showMsg('提示', "请选择需要删除的人员");
                return false;
            }
            if (confirm("是否确定删除？")) {
                $loading.show();
                $http({
                    method: "post",
                    url: "../../api/leaderadmin/delete",
                    data: {
                        userIds: ids.join(",")
                    }
                }).then(function (result) {
                    $loading.hide();
                    if (result.data.success) {
                        $scope.showMsg('成功', "操作成功");
                        $scope.getList();
                    } else {
                        $scope.showMsg('失败', result.data.Message);
                    }
                }, function (resp) {
                    $loading.hide();
                    $scope.showMsg('错误', '服务器错误：' + resp.data);
                });
            }
        };

        $scope.$watch("selectparams.pageindex", function (newVal, oldVal) {
            if ($scope.pageInfo.ptotal > 0) {
                $scope.getList();
            }
        });

        $scope.getList();

    });
})(angular);